const db = require('../db/database');

class SysDao {

    constructor(database){
        this.database = database;
    }

    createTable(){
        const sql = `
            create table if not exists sys (
                id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
                name TEXT,
                sysgroup INTEGER,
                ip TEXT,
                port TEXT,
                username TEXT,
                password TEXT,
                mode INTEGER,
                desc TEXT,
                updateTime TEXT
            )
        `;
        return this.database.run(sql);
    }

    insert(params){
        const sql = `
            insert into sys(name, sysgroup, ip, port, username, password, mode, desc, updateTime)
            values(?, ?, ?, ?, ?, ?, ?, ?, ?)
        `;
        return this.database.run(sql, [params.name, params.sysgroup, params.ip, params.port, 
            params.username, params.password, params.mode, params.desc, new Date()]);
    }

    update(params){
        const {id} = params;
        const sql = `
            update sys
            set name=?,sysgroup=?,ip=?,port=?,username=?,password=?,desc=?,updateTime=?
            where id=?
        `;
        return this.database.run(sql,[
            params.name,
            params.sysgroup,
            params.ip,
            params.port,
            params.username,
            params.password,
            params.desc,
            new Date(),
            id
        ]);
    }

    delete(id){
        const sql = `
            delete from sys where id=?
        `;
        return this.database.run(sql, [id]);
    }

    all(params){
        const sql = `
            select * from sys where mode=?
        `;
        return this.database.all(sql, [params.mode]);
    }

    getById(id){
        const sql = `
            select * from sys where id=?
        `;
        return this.database.get(sql, [id]);
    }

}

const sysDao = new SysDao(db);

module.exports = sysDao;